#include "UserInfoLayer.h"
#include "Game/GameLib.h"

bool UserInfoLayer::init()
{
	if (!Layer::init()) {
		return false;
	}

// 	LayerColor* layerColor = LayerColor::create(Color4B(0, 0, 0, 180));
// 	addChild(layerColor);
// 	layerColor->setContentSize(Director::getInstance()->getVisibleSize());

	layoutMain = CSLoader::createNode("userInfoLayer2.csb");
	addChild(layoutMain);

	auto btn = (Button*)layoutMain->getChildByName("Button_close");
	btn->addTouchEventListener(CC_CALLBACK_2(UserInfoLayer::touchEvent, this));
	return true;
}

bool UserInfoLayer::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *unused_event)
{
// 	if (!layoutMain->getChildByName("Panel_chat")->getBoundingBox().containsPoint(touch->getLocation()))
// 	{
// 		CCLOG("------------------UserInfoLayer::onTouchBegan---------------");
// 		this->runAction(RemoveSelf::create());
// 	}
	return true;
}

void UserInfoLayer::onEnterTransitionDidFinish()
{
	Layer::onEnterTransitionDidFinish();

	Sprite* headImage = (Sprite*)layoutMain->getChildByName("Sprite_head");
	if (UserInfo::Instance().getHeadHttp() != "")
	{
		ImagicDownManager::Instance().addDown(headImage,
			UserInfo::Instance().getHeadHttp(), UserInfo::Instance().getUserID());
	}
	else
	{
		if (UserInfo::Instance().getGender() == 0)
			headImage->setTexture("common/Head0.png");
		else
			headImage->setTexture("common/Head1.png");
		headImage->setScale(1.5);
	}

	Text* nickName = (Text*)layoutMain->getChildByName("Text_nickName");
	nickName->setString(UserInfo::Instance().getUserNicName()); 

	Text* userId = (Text*)layoutMain->getChildByName("Text_userID");
	//userId->setString(StringUtils::format(utility::getScriptString("word_userInfo_id").c_str(),UserInfo::Instance().getUserID()));	
	userId->setString(StringUtils::format("%d", UserInfo::Instance().getUserID()));
	
	//Text* ingotNum = (Text*)layoutMain->getChildByName("Text_ingot");
	//ingotNum->setString(StringUtils::format(utility::getScriptString("word_userInfo_ingot").c_str(), UserInfo::Instance().getUserScore()));

	Text* zuanshiNum = (Text*)layoutMain->getChildByName("Text_fangka");
	//zuanshiNum->setString(StringUtils::format(utility::getScriptString("word_userInfo_zuanshi").c_str(), UserInfo::Instance().getUserDomain()));
	zuanshiNum->setString(StringUtils::format("%d", UserInfo::Instance().getUserDomain()));
}

void UserInfoLayer::onEnter()
{
	Layer::onEnter();
	auto listener = EventListenerTouchOneByOne::create();
	listener->setSwallowTouches(true);
	listener->onTouchBegan = CC_CALLBACK_2(UserInfoLayer::onTouchBegan, this);

	_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
	_touchListener = listener;
}

void UserInfoLayer::onExit()
{
	_eventDispatcher->removeEventListener(_touchListener);
	_touchListener = nullptr;
	Layer::onExit();
}

void UserInfoLayer::touchEvent(cocos2d::Ref *pSender, cocos2d::ui::Widget::TouchEventType type)
{
	Button* btn = (Button*)pSender;
	if (!btn) return;
	switch (type) {
	case Widget::TouchEventType::ENDED:
	{
		if (btn->getName().compare("Button_close") == 0)
		{
			SoundFun::playEffect("effect_close.mp3");
			this->removeFromParentAndCleanup(true);
		}
		break;
	}
	default:
		break;
	}
}